package com.iotechn.unimall.admin.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.CouponDO;
import com.iotechn.unimall.data.domain.RechargeableDO;
import com.iotechn.unimall.data.enums.RechargeRewardType;
import com.iotechn.unimall.data.enums.coupon.CouponType;
import com.iotechn.unimall.data.mapper.CouponMapper;
import com.iotechn.unimall.data.mapper.RechargeableMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class RechargeableServiceImpl implements RechargeableService {
    @Autowired
    private RechargeableMapper rechargeableMapper;
    @Autowired
    private CouponMapper couponMapper;

     @Override
        public Page<RechargeableDO> page(Integer page, Integer limit, Long adminId) throws ServiceException {
            Wrapper<RechargeableDO> wrapper = new EntityWrapper<RechargeableDO>();
         wrapper.eq("status",1);
         wrapper.orderBy("id", false);
            Integer count = rechargeableMapper.selectCount(wrapper);
            List<RechargeableDO> RechargeableDOS = rechargeableMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
            return new Page<>(RechargeableDOS, page, limit, count);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public RechargeableDO create(RechargeableDO rechargeableDO, Long adminId) throws ServiceException {
//            rechargeableDO.setAdminId(adminId);
            Date now=new Date();
            rechargeableDO.setGmtCreate(now);
            rechargeableDO.setGmtUpdate(now);
            rechargeableDO.setStatus(1);
            if (rechargeableMapper.insert(rechargeableDO) > 0) {
                rechargeableDO.setId(rechargeableDO.getId());
                return rechargeableDO;
            }
             throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public String update(RechargeableDO rechargeableDO, Long adminId) throws ServiceException {
            Long id = rechargeableDO.getId();
            if (id == null) {
               return "fail";
            }
            Date now=new Date();
            rechargeableDO.setGmtUpdate(now);
            if(RechargeRewardType.REWARD.getCode()==rechargeableDO.getRechargeRewardType() ){
                rechargeableDO.setRewardCouponId(0l);
            }
            if(RechargeRewardType.REWARDCOUPON.getCode()==rechargeableDO.getRechargeRewardType()){
                rechargeableDO.setRewardAmount(0);
            }
            rechargeableDO.getRechargeRewardType();
            if (rechargeableMapper.updateById(rechargeableDO) > 0) {
                return "ok";
            }
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        RechargeableDO rechargeableDO=new RechargeableDO();
        rechargeableDO.setId(id);
        rechargeableDO.setStatus(0);
        if (rechargeableMapper.updateById(rechargeableDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

     }

    @Override
    public RechargeableDO detail(Long id, Long adminId) throws ServiceException {
       return rechargeableMapper.selectById(id);
    }

    @Override
    public List<CouponDO> getRewardCoupon(Long adminId) {
        System.out.println("!!!!!!获取优惠券列表!!!!!!!");
        List<CouponDO> CouponDOList = couponMapper.selectList(new EntityWrapper<CouponDO>().eq("type", CouponType.REWARD_COUPON.getCode()));
        return CouponDOList;
    }
}